﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using DOAN.DO;
using DOAN.BL;

namespace DOAN.Admin
{
    public partial class QL_Giaovien : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["quyen"] != null)
            {
                string magv;
                if (Session["quyen"] != null)
                {
                    if ((Session["quyen"].ToString() == "AD") || (Session["quyen"].ToString() == "GV"))
                    {

                        if (Session["Tendangnhap"] != null && Session["Tendangnhap"].ToString() != "")
                        {

                            MasterPage mt = this.Master as MasterPage;
                            LinkButton lb = mt.FindControl("LinkButton1") as LinkButton;
                            lb.Visible = false;
                            Label bl = mt.FindControl("lblDnthanhcong") as Label;
                            bl.Visible = true;
                            Label blten = mt.FindControl("lbtendn") as Label;
                            blten.Visible = true;
                            blten.Text = Session["Tendangnhap"].ToString();
                            LinkButton lbDangky = mt.FindControl("lbDangky") as LinkButton;
                            lbDangky.Visible = false;
                            LinkButton lbthoat = mt.FindControl("lbThoat") as LinkButton;
                            lbthoat.Visible = true;

                        }

                        if (!IsPostBack)
                        {
                            magv = Session["MaTK"].ToString();
                            napGV();
                            napns();
                            napns2();
                            //napGV_theoma(magv);
                            pnThemgv.Visible = false;
                        }
                    }
                    else
                    {
                        Response.Redirect("~/Default.aspx");
                    }
                }
            }
            else
            {
                Response.Redirect("LoginAdmin.aspx");
            }
        }
        private void napGV()
        {
            DataTable dt = new ThanhvienBL().GetGV();
            try
            {
                gridSuaGV.DataSource = dt;
                gridSuaGV.DataBind();

            }
            catch (Exception ee)
            {
                lblLoi.Text = ee.Message;
            }
            finally
            {
                dt.Dispose();
            }
        }
        // NapGV_ma()
        private void napGV_theoma()
        {
            try
            {
                gridSuaGV.DataSource = new ThanhvienBL().GetGV();
                gridSuaGV.DataBind();

            }
            catch (Exception ee)
            {
                lblLoi.Text = ee.Message;
            }
            finally
            {
            }
        }
        // GV theo ma
        private void Lay_GV(string magv)//thuc hien chuc nang bind data 
        {
            ThanhvienDO obj = new ThanhvienBL().Select(new ThanhvienDO { Tentaikhoan = magv });
            try
            {
                lblmatv.Text = obj.Tentaikhoan;
                txtTentaikhoan.Text = obj.Hovaten;
                //TxtHoten.Text = Convert.ToString(mBAL.Hienthitv_theoma(ma).Rows[0].ItemArray[6]);
                txtsdt.Text = obj.Lop;
                txtMk.Text = obj.Email;
                TxtHoten.Text = obj.Matkhau;
                // txtEmailtk.Text = Convert.ToString(mBAL.Hienthitv_theoma(ma).Rows[0].ItemArray[4]);
                TxtLop.Text = obj.Sodienthoai;
                string s = obj.Ngaysinh;
                try
                {
                    int ngay = int.Parse(s.Substring(0, s.IndexOf("/")));
                    s = s.Substring(s.IndexOf("/") + 1);
                    int thang = int.Parse(s.Substring(0, s.IndexOf("/")));
                    int nam = int.Parse(s.Substring(s.IndexOf("/") + 1));

                    drNgaysinh.SelectedValue = ngay.ToString();
                    drThangsinh.SelectedValue = thang.ToString();
                    drNamsinh.SelectedValue = nam.ToString();
                }
                catch (Exception s1)
                {
                }
            }

            catch (Exception ee)
            {
                lblLoi.Text = ee.Message;
            }
            finally
            {

            }
        }
        // Sua gv
        protected void gridSuaGV_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string ma = Convert.ToString(e.CommandArgument);
            int result = 0;
            try
            {
                if (e.CommandName == "xoa")
                {
                    result = new ThanhvienBL().Delete(new ThanhvienDO { Tentaikhoan = ma });
                    if (result > 0)
                    {
                        lblLoi.Text = "Xóa thành công !";
                        napGV();
                    }
                    else
                    {
                        lblLoi.Text = "Xóa không thành công !";
                    }
                }
                else
                {
                    if (e.CommandName == "sua")
                    {
                        pnThemgv.Visible = false;
                        pnSuaGV.Visible = true;
                        Lay_GV(ma);
                        lblLoi.Visible = false;
                    }
                }
            }
            catch (Exception ee)
            {
                lblLoi.Text = ee.Message;
            }
            finally
            {
            }
        }

        private string ngaysinhs()
        {
            string dd = Convert.ToString(drNgaysinh.SelectedItem.Value);
            string mm = Convert.ToString(drThangsinh.SelectedItem.Value);
            string yy = Convert.ToString(drNamsinh.SelectedItem.Value);
            string gach = "/";
            return dd + gach + mm + gach + yy;
        }

        private int gioitinh()
        {
            //int gioitinh = Convert.ToInt32(rdGioitinh.SelectedItem.Value);
            int gioitinh = 1;
            if (Convert.ToInt32(rdGioitinh.SelectedItem.Value) == 1)//lay gia tri gioi tinh :nam=1,nu =0
            {
                gioitinh = 1;
            }
            if (Convert.ToInt32(rdGioitinh.SelectedItem.Value) == 2)
            {
                gioitinh = 0;

            }
            return gioitinh;

        }

        private void napns()
        {

            for (int i = 1; i <= 12; i++)
            {
                drThangsinh.Items.Add(i.ToString());
            }
            for (int i = 1; i <= 31; i++)
            {
                drNgaysinh.Items.Add(i.ToString());
            }
            for (int i = 1950; i <= 1995; i++)
            {
                drNamsinh.Items.Add(i.ToString());
            }
        }


        //.......................................................................................Thêm


        private string ngaysinhs2()
        {
            string dd = Convert.ToString(drNgaysinh2.SelectedItem.Value);
            string mm = Convert.ToString(drThangsinh2.SelectedItem.Value);
            string yy = Convert.ToString(drNamsinh2.SelectedItem.Value);
            string gach = "/";
            return dd + gach + mm + gach + yy;
        }

        private int gioitinh2()
        {
            //int gioitinh = Convert.ToInt32(rdGioitinh.SelectedItem.Value);
            int gioitinh2 = 1;
            if (Convert.ToInt32(rdGioitinh2.SelectedItem.Value) == 1)//lay gia tri gioi tinh :nam=1,nu =0
            {
                gioitinh2 = 1;
            }
            if (Convert.ToInt32(rdGioitinh2.SelectedItem.Value) == 2)
            {
                gioitinh2 = 0;

            }
            return gioitinh2;

        }

        private void napns2()
        {

            for (int i = 1; i <= 12; i++)
            {
                drThangsinh2.Items.Add(i.ToString());
            }
            for (int i = 1; i <= 31; i++)
            {
                drNgaysinh2.Items.Add(i.ToString());
            }
            for (int i = 1950; i <= 1995; i++)
            {
                drNamsinh2.Items.Add(i.ToString());
            }
        }



        protected void btDongy_Click(object sender, EventArgs e)
        {
            ThanhvienDO obj = new ThanhvienBL().Select(new ThanhvienDO{Tentaikhoan = lblmatv.Text});
            int kq = 0;
            int result = 0;
            obj.Tentaikhoan = lblmatv.Text.ToString();
            obj.Hovaten = txtTentaikhoan.Text;
            obj.Matkhau = TxtHoten.Text;
            obj.Lop = txtsdt.Text;
            obj.Sodienthoai = TxtLop.Text;
            obj.Email = txtMk.Text;
            string ngaysinh = ngaysinhs();
            int gt = gioitinh();
            obj.Maquyen = "GV";
            try
            {
                kq = new ThanhvienBL().Update(obj);
                //string magv, string maquyen, string hoten, string matkhau, string email, string ngaysinh, string gioitinh, string lop, string sodienthoai)
                {
                    if (kq == 0)
                    {
                        lblLoi.Text = "Update thanh cong !";;
                        napGV_theoma();
                        pnSuaGV.Visible = false;
                    }
                    else
                    {
                        lblLoi.Text = "Update khong thanh cong !";
                    }
                }
            }
            catch (Exception ex)
            {
                lblLoi.Text = ex.Message;
            }
            finally
            {

            }
        }

        protected void btHuy_Click(object sender, EventArgs e)
        {
            pnSuaGV.Visible = false;

        }
        protected void imgRe_Click(object sender, ImageClickEventArgs e)
        {
            napGV();
            //gridDSGV.Visible = true;
            pnThemgv.Visible = false;
            pnSuaGV.Visible = false;
            gridSuaGV.Visible = true;
        }
        protected void imgThem_Click(object sender, ImageClickEventArgs e)
        {
            pnThemgv.Visible = true;
            pnSuaGV.Visible = false;
        }
        protected void imgSuagv_Click(object sender, ImageClickEventArgs e)
        {
            pnSuaGV.Visible = true;
            pnThemgv.Visible = false;
            string magv = Session["MaTK"].ToString();
            napGV_theoma();
            //gridDSGV.Visible = false;
            gridSuaGV.Visible = true;
        }

        protected void imgXemgv_Click(object sender, ImageClickEventArgs e)
        {
            napGV();
            pnThemgv.Visible = false;
            //gridDSGV.Visible = true;
            pnSuaGV.Visible = false;
            gridSuaGV.Visible = true;
        }


        protected void imgTimkiem_Click(object sender, ImageClickEventArgs e)
        {

        }
        protected void btLuu_them_Click(object sender, EventArgs e)
        {
            ThanhvienDO obj = new ThanhvienDO();
            int kq = 0;
            obj.Tentaikhoan = txttentk2.Text;
            obj.Hovaten = txthoten2.Text;
            obj.Matkhau = txtmk2.Text;
            obj.Lop = txtlop2.Text;
            obj.Sodienthoai = txtsdt2.Text;
            obj.Email = txtemail2.Text;
            string ngaysinh2 = ngaysinhs2();
            int gt2 = gioitinh2();
            string maquyen2 = "GV";
            obj.Maquyen = maquyen2;
            obj.Gioitinh = gt2;
            obj.Ngaysinh = ngaysinh2;
            try
            {
                //if (new ThanhvienBL().Select(new ThanhvienDO { Tentaikhoan = txttentk2.Text }).Maquyen != null)
                    //lblLoi.Text = "Da ton tai ten dang nhap!";
                //else
              //  {
                    kq = new ThanhvienBL().Insert(obj);
                    if (kq == 0)
                    {
                        lblLoi.Text = "Thêm thành công!";
                        napGV();
                        //pnYkien.Visible = false;
                        pnThemgv.Visible = false;
                    }
                    else
                    {
                        lblLoi.Text = "Thêm không thành công !";
                    }
               // }

            }
            catch (Exception ex)
            {
                lblLoi.Text = ex.Message;
            }
            finally
            {

            }
        }
        protected void btHuy_luu_Click(object sender, EventArgs e)
        {
            pnThemgv.Visible = false;
        }

        protected void gridSuaGV_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridSuaGV.PageIndex = e.NewPageIndex;
            this.napGV();
        }
    }


}
